<template>
    <div>
        <div>
            <el-col :span="3">
                <div class="grid-content bg-purple"></div>
                <el-button type="primary" @click="onhandAdd">添加菜品</el-button>
            </el-col>
            <el-input style="width: 240px;" placeholder="请输入菜名" v-model="params.name"></el-input>
            <el-button style="margin-left: 10px;" type="primary" icon="el-icon-search " @click="loads">搜索</el-button>
            <el-button style="margin-left: 10px;" type="warning" icon="el-icon-scissors " @click="reset">重置</el-button>
        </div>
        <br>
        <div>
            <el-col :span="6">
                <div class="grid-content bg-purple"></div>
                <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                               title="确认删除所有选中的菜品吗？" @confirm="deleteSelected">
                    <el-button type="danger" slot="reference" v-bind:disabled="disable">删除所选菜品</el-button>
                </el-popconfirm>
            </el-col>
        </div>
        <el-table ref="multipleTable" :data="tableData" stripe style="width: 100%" tooltip-effect="dark"
                  @selection-change="handleSelectionChange">
            <el-table-column
                    prop="id"
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="id" label="菜品编号" width="180">
            </el-table-column>
            <el-table-column prop="name" label="菜名" width="180">
            </el-table-column>
            <el-table-column prop="price" label="价格" width="180">
            </el-table-column>
            <el-table-column prop="costPrice" label="成本价" width="180">
            </el-table-column>
            <el-table-column prop="description" label="描述" width="180">
            </el-table-column>
            <el-table-column align="center" label="图片" prop="picture" width="100px">
                <template v-slot="scope">
                    <el-avatar :size="45" :src="scope.row.picture" shape="square"></el-avatar>
                </template>
            </el-table-column>
            <el-table-column label="操作" prop="id">
                <template v-slot="scope">
                    <el-button type="primary" @click="handleEdit(scope.row.id)">编辑
                    </el-button>
                    <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                                   title="确认删除该菜品吗？" @confirm="del(scope.row.id)">
                        <el-button type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
            <el-button @click="toggleSelection()">取消选择</el-button>
        </div>
        <!-- 分页 -->
        <div style="margin-top: 10px;">
            <el-pagination background :current-page="params.pageNum" :page-size="params.pageSize"
                           layout="prev, pager, next" :total="total" @current-change="handleCurrentChange">
            </el-pagination>
        </div>
        <!-- 新增编辑弹框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form :inline="true" :model="deskform" status-icon ref="deskform"
                     style="width: 80%;"
                     label-width="200px">
                <el-form-item label="菜名" prop="name">
                    <el-input v-model="deskform.name" placeholder="请输入菜名"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input v-model="deskform.price" placeholder="请输入价格"></el-input>
                </el-form-item>
                <el-form-item label="成本价" prop="costPrice">
                    <el-input v-model="deskform.costPrice" placeholder="请输入成本价"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input v-model="deskform.description" placeholder="请输入菜品描述"></el-input>
                </el-form-item>
                <el-form-item label="图片" prop="image">
                    <div>
                        <el-upload
                            :on-success="handleAvatarSuccess"
                            :show-file-list="false"
                            action="http://localhost:8081/file/upload"
                            class="avatar-uploader">
                            <el-avatar
                                v-if="deskform.picture"
                                :size="110"
                                :src="deskform.picture"
                                shape="square"
                            >
                            </el-avatar>
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </div>
                </el-form-item>
            </el-form>
            <div style="text-align-last: center;width: 60%;">
                <el-button @click="closedialog('deskform')" type="danger">取 消</el-button>
                <el-button @click="resetForm('deskform')" type="warning">重置</el-button>
                <el-button style="margin-right: 30px;" type="primary" icon="el-icon-upload2 "
                           @click="dialogVisibleConfirm('deskform')" size="medium">{{save}}
                </el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from '@/request'

    export default {
        name: 'desk',
        data() {
            return {
                //将所有选中的id存全局
                multipleSelection: [],
                //后端返回的数据
                tableData: [],
                //后端返回的页数
                total: 0,
                //id存全局，判断以新增还是修改出现dialog
                allid: "",
                //dialog的标题
                title: "",
                //dialog的按钮
                save: '',
                //判断删除所选是否可用
                disable: true,
                //判断dialog是否显示
                dialogVisible: false,
                //分页模糊查询的请求体
                params: {
                    pageNum: 1,
                    pageSize: 8,
                },
                //dialog的表单
                deskform: {
                   name: '',
                    price:'',
                    costPrice:'',
                    description: '',
                    picture: ''
                },
            }
        },
        created() {
            this.load()
        },
        methods: {
            handleAvatarSuccess(res) {
                console.log(res)
                this.deskform.picture = res;
            },
            //修改封面
            handleAvatarSuccess111(res) {
                this.deskform.picture = res;
            },
            //取消所有多选
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            //判断删除所选按钮是否可用以及将已选id存全局
            handleSelectionChange(val) {
                if (val.length == 0) {
                    this.multipleSelection.length = val.length
                    this.disable = true
                } else {
                    this.disable = false
                    for (let i = 0; i < val.length; i++) {
                        this.multipleSelection[i] = val[i].deskId
                    }
                }
            },
            //新增方法
            add(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        request.post('/caipin/add', this.deskform).then(res => {
                            if (res.code === '200') {
                                this.$notify.success('新增成功')
                                this.resetForm(formName) //清空表格数据
                                this.dialogVisible = false; //关闭弹框
                                this.load(); //刷新列表
                            } else {
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                });
            },
            //修改方法
            update(formName) {
                request.put('/caipin/update', this.deskform).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('修改成功')
                        this.resetForm(formName) //清空表格数据
                        this.dialogVisible = false; //关闭弹框
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            //删除方法
            del(chefAdminId) {
                request.delete('/caipin/delete' + chefAdminId).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('删除成功')
                        this.load();
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            //重置密码方法
            resetPassword(chefAdminId) {
                request.put('/chefadmin/resetpassword' + chefAdminId).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('重置成功，初始密码为123')
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            //批量删除方法
            deleteSelected() {
                console.log(this.multipleSelection)
                request.post('/caipin/deletes', this.multipleSelection).then(res => {
                    console.log(res)
                    if (res.code === '200') {
                        this.$notify.success('批量删除成功')
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            //关闭dialog
            handleClose(done) {
                this.dialogVisible = false;
            },
            //添加按钮，从此处调取添加方法
            onhandAdd() {
                this.title = '添加餐桌';
                this.save = "添加"
                this.resetForm('deskform');
                this.dialogVisible = true;
                this.allid = ''
                this.deskform = {
                    name: '',
                        price:'',
                        costPrice:'',
                        description: '',
                        picture: ''
                }
            },
            //编辑按钮，从此处调取修改方法
            handleEdit(chefAdminId) {
                this.save = "保存"
                this.title = '修改餐桌信息'
                this.dialogVisible = true; //显示弹框
                request.get("/caipin/" + chefAdminId).then(res => {
                    this.deskform = res.data

                })//row当前行数据，把当前行的数据赋值给 表单
                this.allid = chefAdminId; //把id存全局
            },
            //清空表单
            resetForm(formName) {
                this.$nextTick(() => {
                    this.$refs[formName].resetFields();
                })
            },
            //页面加载
            loads() {
                this.params.pageNum = 1
                this.load()
            },
            //初始化页面
            load() {
                request.get('/caipin/page', {
                    params: this.params
                }).then(res => {
                    if (res.code === "200") {
                        this.total = res.data.total;
                        this.tableData = res.data.list;
                    }
                })

            },
            //重置方法
            reset() {
                this.params = {
                    user_name: '',
                    user_num: '',
                    pageNum: 1,
                    pageSize: 10
                }
                this.load()
            },
            //核心判断哪一个按钮调用表单组件
            dialogVisibleConfirm(formName) {
                // 新增接口
                if (!this.allid) {
                    this.add(formName)

                } else {
                    // 修改接口
                    this.update(formName);
                }
            },
            //关闭dialog
            closedialog(formName) {
                this.dialogVisible = false;
                this.resetForm(formName) //清空表格数据
            },
            //切换页码
            handleCurrentChange(pageNum) {
                this.params.pageNum = pageNum;
                this.load()
            }
        }
    }
</script>
